package aynu.hd.Admin.Service.ServiceImpl;

import aynu.hd.Admin.Dao.AdminRoomDao;
import aynu.hd.Admin.Service.AdminRoomService;
import aynu.hd.Hotel.Dao.orderDao;
import aynu.hd.Hotel.Dao.roomDao;
import aynu.hd.Hotel.Dao.scheduleDao;
import aynu.hd.pojo.Orders;
import aynu.hd.pojo.Room;
import aynu.hd.pojo.Schedule;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AdminRoomServiceImpl implements AdminRoomService {
    @Autowired
    AdminRoomDao adminRoomDao;
    @Autowired
    scheduleDao scheduleDao;
    @Autowired
    orderDao orderDao;
    @Autowired
    roomDao roomDao;

    //查询六天的日期
    @Transactional
    @Override
    public List queryDate() {
        List list = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        //当天
        String date1 = sdf.format(new Date());
        //前1天
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        String date2 = sdf.format(calendar.getTime());
        //前2天
        calendar.add(calendar.DATE, -1);
        String date3 = sdf.format(calendar.getTime());
        //前3天
        calendar.add(calendar.DATE, -1);
        String date4 = sdf.format(calendar.getTime());
        //前4天
        calendar.add(calendar.DATE, -1);
        String date5 = sdf.format(calendar.getTime());
        //前5天
        calendar.add(calendar.DATE, -1);
        String date6 = sdf.format(calendar.getTime());
        //前6天
        calendar.add(calendar.DATE, -1);
        String date7 = sdf.format(calendar.getTime());
        //前7天
        calendar.add(calendar.DATE, -1);
        String date8 = sdf.format(calendar.getTime());
        list.add(date8);
        list.add(date7);
        list.add(date6);
        list.add(date5);
        list.add(date4);
        list.add(date3);
        list.add(date2);
        list.add(date1);
        return list;
    }

    //查询六天的销售金额
    @Transactional
    @Override
    public List queryMoneyDay(List dates, Model model) {
        List listMoney = new ArrayList();
        double height = 10000;
        for (Object date : dates) {
            Integer orders_money = adminRoomDao.queryMoneyDay(date.toString());
            if (orders_money == null) {
                listMoney.add("0");
            } else {
                listMoney.add(orders_money);
                if (orders_money > height) {
                    height = orders_money;
                }
            }
        }
        int height1;
        if (height != 10000) {
            height1 = (int) (height + 10);
        } else {
            height1 = (int) (height);
        }
        model.addAttribute("height", height1);
        return listMoney;
    }

    //查询未操作评价数量
    @Transactional
    @Override
    public Integer queryCommentSize() {
        List<Schedule> schedules = adminRoomDao.queryCommentSize();
        return schedules.size();
    }

    //房间评论消息未处理查看
    @Transactional
    @Override
    public List<Schedule> queryRoomComment() {
        return adminRoomDao.queryCommentSize();
    }

    //设置评论不发布
    @Transactional
    @Override
    public String reduceScheduleComment(String schedule_id, String state) {
        adminRoomDao.reduceScheduleComment(schedule_id, state);
        return "OK";
    }

    //房间评论消息已经处理
    @Transactional
    @Override
    public List<Schedule> queryRoomComment1() {
        return adminRoomDao.queryCommentSize1();
    }

    //订单数量
    @Transactional
    @Override
    public Integer queryOrdersSize() {
        return adminRoomDao.queryOrdersSize();
    }

    //房间数量
    @Transactional
    @Override
    public Integer queryRoomSize() {
        return adminRoomDao.queryRoomSize();
    }

    //用户数量
    @Transactional
    @Override
    public Integer queryUserSize() {
        return adminRoomDao.queryUserSize();
    }

    //查询全部订单（未支付）
    @Transactional
    @Override
    public List<Orders> orderListNo() {
        return adminRoomDao.orderListNo();
    }

    //查询全部订单（已支付）
    @Transactional
    @Override
    public List<Orders> orderListYes() {
        return adminRoomDao.orderListYes();
    }

    //删除订单
    @Override
    @Transactional
    public String removeOrders(String orders_id) {
        Schedule schedule = scheduleDao.queryScheduleByOrderId(Integer.valueOf(orders_id));
        if (schedule.getOrders() != null) {
            orderDao.deleteOrderById(schedule.getOrders().getOrders_id());
        }
        scheduleDao.removeSchedule(schedule.getSchedule_id().toString());
        return "OK";
    }

    //查找所有用户信息
    @Transactional
    @Override
    public List<User> userList() {
        return adminRoomDao.userList();
    }

    //新增房间
    @Transactional
    @Override
    public int roomAdd(Room room) {
        return adminRoomDao.roomAdd(room);
    }

    //查询全部存在的房间
    @Transactional
    @Override
    public List<Room> queryRoomAll() {
        return roomDao.queryRoomByType(null);
    }

    //删除房间
    @Transactional
    @Override
    public String roomDelete(String room_id) {
        adminRoomDao.roomDelete(room_id);
        scheduleDao.removeScheduleByRoomId(room_id);
        return "OK";
    }

    //根据id查找房间
    @Transactional
    @Override
    public Room roomModifyPage(String room_id) {
        return roomDao.queryRoomById(room_id);
    }

    //修改房间信息
    @Transactional
    @Override
    public int roomModify(Room room) {
        return adminRoomDao.roomModify(room);
    }

    //订单对应的预定信息
    @Override
    @Transactional
    public List<Schedule> scheduleListYes() {
        String[] ids = adminRoomDao.orderListIdYes();
        return adminRoomDao.queryScheduleByOrders(ids);
    }
}
